home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / The GIMP 2.2.8 / gimp-2.2.8-i586-setup.exe / {app} / share / gimp / 2.0 / scripts / perspective-shadow.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2005-06-30  |  6.1 KB  |  197 lines

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 2 of the License, or
  6. ; (at your option) any later version.  
  7. ; This program is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with this program; if not, write to the Free Software
  13. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. ;
  15. ;
  16. ; perspective-shadow.scm   version 1.2   2000/11/08
  17. ;
  18. ; Copyright (C) 1997-2000 Sven Neumann <sven@gimp.org>
  19. ;  
  20. ; Adds a perspective shadow of the current selection or alpha-channel 
  21. ; as a layer below the active layer
  22. ;
  23.   
  24. (define (script-fu-perspective-shadow image
  25.                       drawable
  26.                       alpha
  27.                       rel-distance
  28.                       rel-length
  29.                       shadow-blur
  30.                       shadow-color
  31.                       shadow-opacity
  32.                       interpolation
  33.                       allow-resize)
  34.   (let* ((shadow-blur (max shadow-blur 0))
  35.      (shadow-opacity (min shadow-opacity 100))
  36.      (shadow-opacity (max shadow-opacity 0))
  37.      (rel-length (abs rel-length))
  38.      (alpha (* (/ alpha 180) *pi*))
  39.      (type (car (gimp-drawable-type-with-alpha drawable)))
  40.      (image-width (car (gimp-image-width image)))
  41.      (image-height (car (gimp-image-height image)))
  42.      (from-selection 0)
  43.      (active-selection 0)
  44.      (shadow-layer 0))
  45.     
  46.   (if (= rel-distance 0) (set! rel-distance 999999))
  47.  
  48.   (gimp-context-push)
  49.   
  50.   (gimp-image-undo-group-start image)
  51.   
  52.   (gimp-layer-add-alpha drawable)
  53.   (if (= (car (gimp-selection-is-empty image)) TRUE)
  54.       (begin
  55.     (gimp-selection-layer-alpha drawable)
  56.     (set! from-selection FALSE))
  57.       (begin
  58.     (set! from-selection TRUE)
  59.     (set! active-selection (car (gimp-selection-save image)))))
  60.   
  61.   (let* ((selection-bounds (gimp-selection-bounds image))
  62.      (select-offset-x (cadr selection-bounds))
  63.      (select-offset-y (caddr selection-bounds))
  64.      (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
  65.      (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
  66.  
  67.      (abs-length (* rel-length select-height))
  68.      (abs-distance (* rel-distance select-height))
  69.      (half-bottom-width (/ select-width 2))
  70.      (half-top-width (* half-bottom-width
  71.               (/ (- rel-distance rel-length) rel-distance)))
  72.   
  73.      (x0 (+ select-offset-x (+ (- half-bottom-width half-top-width)
  74.                  (* (cos alpha) abs-length))))
  75.      (y0 (+ select-offset-y (- select-height
  76.                  (* (sin alpha) abs-length))))
  77.      (x1 (+ x0 (* 2 half-top-width)))
  78.      (y1 y0)
  79.      (x2 select-offset-x)
  80.      (y2 (+ select-offset-y select-height))
  81.      (x3 (+ x2 select-width))
  82.      (y3 y2)
  83.  
  84.      (shadow-width (+ (- (max x1 x3) (min x0 x2)) (* 2 shadow-blur)))
  85.      (shadow-height (+ (- (max y1 y3) (min y0 y2)) (* 2 shadow-blur)))
  86.      (shadow-offset-x (- (min x0 x2) shadow-blur))
  87.      (shadow-offset-y (- (min y0 y2) shadow-blur)))
  88.      
  89.   
  90.     (set! shadow-layer (car (gimp-layer-new image
  91.                         select-width
  92.                         select-height
  93.                         type
  94.                         "Perspective Shadow"
  95.                         shadow-opacity
  96.                         NORMAL-MODE)))
  97.  
  98.  
  99.     (gimp-image-add-layer image shadow-layer -1)
  100.     (gimp-layer-set-offsets shadow-layer select-offset-x select-offset-y)
  101.     (gimp-drawable-fill shadow-layer TRANSPARENT-FILL)
  102.     (gimp-context-set-background shadow-color)
  103.     (gimp-edit-fill shadow-layer BACKGROUND-FILL)
  104.     (gimp-selection-none image)
  105.  
  106.     (if (= allow-resize TRUE)
  107.     (let* ((new-image-width image-width)
  108.            (new-image-height image-height)
  109.            (image-offset-x 0)
  110.            (image-offset-y 0))
  111.  
  112.       (if (< shadow-offset-x 0)
  113.           (begin
  114.         (set! image-offset-x (- 0 shadow-offset-x))
  115.         (set! new-image-width (- new-image-width image-offset-x))))
  116.  
  117.       (if (< shadow-offset-y 0)
  118.           (begin
  119.         (set! image-offset-y (- 0 shadow-offset-y))
  120.         (set! new-image-height (- new-image-height image-offset-y))))
  121.       
  122.       (if (> (+ shadow-width shadow-offset-x) new-image-width)
  123.           (set! new-image-width (+ shadow-width shadow-offset-x)))
  124.       
  125.       (if (> (+ shadow-height shadow-offset-y) new-image-height)
  126.           (set! new-image-height (+ shadow-height shadow-offset-y)))
  127.       (gimp-image-resize image
  128.                  new-image-width
  129.                  new-image-height
  130.                  image-offset-x
  131.                  image-offset-y)))
  132.   
  133.     (gimp-drawable-transform-perspective shadow-layer
  134.                      x0 y0
  135.                      x1 y1
  136.                      x2 y2
  137.                      x3 y3
  138.                      TRANSFORM-FORWARD interpolation
  139.                      TRUE 3 FALSE)
  140.  
  141.     (if (>= shadow-blur 1.0)
  142.     (begin
  143.       (gimp-layer-set-preserve-trans shadow-layer FALSE)
  144.       (gimp-layer-resize shadow-layer
  145.                  shadow-width
  146.                  shadow-height
  147.                  shadow-blur
  148.                  shadow-blur)
  149.       (plug-in-gauss-rle 1
  150.                  image
  151.                  shadow-layer
  152.                  shadow-blur
  153.                  TRUE
  154.                  TRUE))))
  155.  
  156.   (if (= from-selection TRUE)
  157.       (begin
  158.     (gimp-selection-load active-selection)
  159.     (gimp-edit-clear shadow-layer)
  160.     (gimp-image-remove-channel image active-selection)))
  161.  
  162.   (if (and
  163.        (= (car (gimp-layer-is-floating-sel drawable)) 0)
  164.        (= from-selection FALSE))
  165.       (gimp-image-raise-layer image drawable))
  166.  
  167.   (gimp-image-set-active-layer image drawable)
  168.   (gimp-image-undo-group-end image)
  169.   (gimp-displays-flush)
  170.  
  171.   (gimp-context-pop)))
  172.  
  173. (script-fu-register "script-fu-perspective-shadow"
  174.             _"_Perspective..."
  175.             "Add a perspective shadow"
  176.             "Sven Neumann <sven@gimp.org>"
  177.             "Sven Neumann"
  178.             "2000/11/08"
  179.             "RGB* GRAY*"
  180.             SF-IMAGE       "Image"          0
  181.             SF-DRAWABLE    "Drawable"       0
  182.             SF-ADJUSTMENT _"Angle"          '(45 0 180 1 10 1 0)
  183.             SF-ADJUSTMENT _"Relative distance of horizon" '(5 0 24 .1 1 1 1)
  184.             SF-ADJUSTMENT _"Relative length of shadow" '(1 0 24 .1 1 1 1)
  185.             SF-ADJUSTMENT _"Blur radius"    '(3 0 1024 1 10 0 0)
  186.             SF-COLOR      _"Color"          '(0 0 0)
  187.             SF-ADJUSTMENT _"Opacity"        '(80 0 100 1 10 0 0)
  188.             SF-OPTION     _"Interpolation"  '(_"None" _"Linear" _"Cubic")
  189.             SF-TOGGLE     _"Allow resizing" FALSE)
  190.  
  191. (script-fu-menu-register "script-fu-perspective-shadow"
  192.              _"<Image>/Script-Fu/Shadow")
  193.